package day07.Texy2;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

interface PersonINf {
    void save();

    void update();

    void add();

    void delect();
}

interface Dog {
    void info();

    void run();
}

class GunDog implements Dog {

    @Override
    public void info() {
        System.out.println("我是一只猎狗");
    }

    @Override
    public void run() {
        System.out.println("我奔跑迅速！");
    }
}

class PersonSQLImp implements PersonINf {
    @Override
    public void save() {
        System.out.println("数据保存成功！");
    }

    @Override
    public void update() {
        System.out.println("数据更新成功！");
    }

    @Override
    public void add() {
        System.out.println("数据添加成功！");
    }

    @Override
    public void delect() {
        System.out.println("数据删除成功！");
    }
}

class AspectJTest {
    public static void before() {
        System.out.println("before!");
    }

    public static void after() {
        System.out.println("after!");
    }
}

class PersonProxy implements InvocationHandler {

    private Object target;

    public PersonProxy(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        AspectJTest.before();

        Object result = method.invoke(target, args);

        AspectJTest.after();

        return result;
    }
}

public class Text {
    public static void main(String[] args) {
        PersonINf target1 = new PersonSQLImp();
        PersonINf result1 = (PersonINf) Proxy.newProxyInstance(
                target1.getClass().getClassLoader(),
                new Class[]{PersonINf.class},
                new PersonProxy(target1));

        result1.save();
        result1.update();
        result1.add();
        result1.delect();

        Dog target2 = new GunDog();
        Dog result2 = (Dog) Proxy.newProxyInstance(
                target2.getClass().getClassLoader(),
                new Class[]{Dog.class},
                new PersonProxy(target2));

        result2.info();
        result2.run();

    }
}

